package com.wheesper.www.WheesperServer.Common;

import ProtocolBuffer.ProtoMessageOuterClass.ProtoMessage.*;
import ProtocolBuffer.ProtoMessageOuterClass.*;

import com.wheesper.www.WheesperServer.Net.Session.IOSession;
import com.wheesper.www.WheesperServer.Services.*;
import com.wheesper.www.WheesperServer.Utils.Logger;

public class RequestDispatcher {
	private TaskQueue<Request> _taskqueue;
	public RequestDispatcher(TaskQueue<Request> _taskqueue) {
		this._taskqueue = _taskqueue;
	}
	
	public void dispacthing() throws InterruptedException {
		Request request;
		IOSession session;
		System.out.println("Dispatching Start!");
		while((request = _taskqueue.take()) != null) {
			ProtoMessage protoMessage = request._protoMessage;
			MsgCase msgCase = protoMessage.getMsgCase();
			session = (IOSession)request._session;
			Logger.Instance().logging("[ProtoBuf Message InCome]{\n"
					+protoMessage.toString()+"\n}");
			//dispatching to services
			switch(msgCase.getNumber()) {
			//注册信息包
			case ProtoMessage.SIGNUPMAILREQUEST_FIELD_NUMBER:
				RegisterServices.handle(session, protoMessage.getSignupMailRequest());
				break;
			case ProtoMessage.SIGNUPCAPTCHAREQUEST_FIELD_NUMBER:
				RegisterServices.handle(session, protoMessage.getSignupCaptchaRequest());
				break;
			//登录信息包
			case ProtoMessage.SIGNUPINFOREQUEST_FIELD_NUMBER:
				RegisterServices.handle(session, protoMessage.getSignupInfoRequest());
				break;
			case ProtoMessage.SIGNOUTREQUEST_FIELD_NUMBER:
				LogServices.handle(session, protoMessage.getSignoutRequest());
				break;
			case ProtoMessage.SIGNINREQUEST_FIELD_NUMBER:
				LogServices.handle(session, protoMessage.getSigninRequest());
				break;
			case ProtoMessage.PASSWORDMODIFYREQUEST_FIELD_NUMBER:
				ModifyPasswordServices.handle(session, protoMessage.getPasswordModifyRequest());
				break;
			case ProtoMessage.PASSWORDMODIFYCAPTCHAREQUEST_FIELD_NUMBER:
				ModifyPasswordServices.handle(session, protoMessage.getPasswordModifyCaptchaRequest());;
				break;
			case ProtoMessage.USERINFOQUERYREQUEST_FIELD_NUMBER:
				UserInfoServices.handle(session, protoMessage.getUserInfoQueryRequest());
				break;
			case ProtoMessage.USERINFOMODIFYREQUEST_FIELD_NUMBER:
				UserInfoServices.handle(session, protoMessage.getUserInfoModifyRequest());
				break;
			case ProtoMessage.CONTACTMAILCHECKREQUEST_FIELD_NUMBER:
				ContactsServices.handle(session, protoMessage.getContactMailCheckRequest());
				break;
			case ProtoMessage.CONTACTLISTREQUEST_FIELD_NUMBER:
				ContactsServices.handle(session, protoMessage.getContactListRequest());
				break;
			case ProtoMessage.CONTACTAPPLYREQUEST_FIELD_NUMBER:
				ContactsServices.handle(session, protoMessage.getContactApplyRequest());
				break;
			case ProtoMessage.CONTACTREPLYREQUEST_FIELD_NUMBER:
				ContactsServices.handle(session, protoMessage.getContactReplyRequest());
				break;
			case ProtoMessage.CONTACTREMARKMODIFYREQUEST_FIELD_NUMBER:
				ContactsServices.handle(session, protoMessage.getContactRemarkModifyRequest());
				break;
			case ProtoMessage.GROUPCREATEAPPLYREQUEST_FIELD_NUMBER:
				GroupServices.handle(session, protoMessage.getGroupCreateApplyRequest());
				break;
			case ProtoMessage.CHATPRIVATEMESSAGEREQUEST_FIELD_NUMBER:
				ChatServices.hanlde(session, protoMessage.getChatPrivateMessageRequest());
				break;
			case ProtoMessage.GROUPPARTICIPATEINREPLYREQUEST_FIELD_NUMBER:
				GroupServices.handle(session, protoMessage.getGroupParticipateInReplyRequest());
				break;
			case ProtoMessage.GROUPPARTICIPATEINAPPLYREQUEST_FIELD_NUMBER:
				GroupServices.handle(session, protoMessage.getGroupParticipateInApplyRequest());
				break;
			case ProtoMessage.GROUPSEARCHREQUEST_FIELD_NUMBER:
				GroupServices.handle(session, protoMessage.getGroupSearchRequest());
				break;
			case ProtoMessage.GROUPLISTREQUEST_FIELD_NUMBER:
				GroupServices.handle(session, protoMessage.getGroupListRequest());
				break;
			default:
				System.out.println("Unhandling ProtoMessage Type!");
				break;
			}
		}
	}
}
